﻿using KMCCC.Authentication;
using KMCCC.Launcher;
using System;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using static Microsoft.VisualBasic.Interaction;
using TcpClientExample;
namespace 测试_Graphs绘图程序
{
    public partial class Form1 : Form
    {
        string ID;
        #region 本程序中用到的API函数        
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwdn, int wMsg, int mParam, int lParam);
        #endregion
        #region 本程序中需要声明的变量 
        public const int WM_SYSCOMMAND = 0x0112;//该变量表示将向Windows发送的消息类型 
        public const int SC_MOVE = 0xF010;//该变量表示发送消息的附加消息         
        public const int HTCAPTION = 0x0002;//该变量表示发送消息的附加消息         
        #endregion

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            var versions = Program.Core.GetVersions().ToArray();
            comboBox1.DataSource = versions;//绑定数据源
            comboBox1.DisplayMember = "Id";//设置comboBox显示的为版本Id
            comboBox1.SelectedIndex = 1;

        }
        public static bool CheckID(string id)
        {
            if (id == "") { return false; }
            if (id == "Arrive_Zenith") { return false; }
            if (id == "kamieee") { return false; }
            if (id == "bt250") { return false; }
            return true;
        }
        
        

        

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g;
            g = this.CreateGraphics();
            Brush bs_black = new SolidBrush(Color.Black);
            Pen pen_white = new Pen(Color.White, 2);
            Rectangle frmFrame = new Rectangle(0, 0, Width, Height);
            g.FillEllipse(bs_black, frmFrame);
            string text1;
            string id = GetSetting("Darkcity", "Darkcity", "ID");
            if (id == "")
            {
                 text1= "欢迎,点我设置游戏名";
            }
            else
            {
                text1 = "欢迎," + id;
                ID = id;
            }
            string text2 = "  登陆\nDarkcity";
            SizeF sizeF = g.MeasureString(text1, new Font("黑体", 14));
            g.DrawString(text1, new Font("黑体", 14), Brushes.White, new Point(150 - (int)sizeF.Width / 2, 50));
            SizeF sizeF2 = g.MeasureString(text2, new Font("黑体", 14));
            g.DrawString(text2, new Font("黑体", 14), Brushes.White, new Point(150 - (int)(sizeF2.Width / 2), 208));
            Rectangle button = new Rectangle(90, 200, 120, 50);
            g.DrawRectangle(pen_white, button);
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            //MessageBox.Show(Convert.ToString(e.Location.X) + " " + Convert.ToString(e.Location.Y));
            Graphics g;
            g = this.CreateGraphics();
            Pen pen_blue = new Pen(Color.DarkOrange, 2);
            Pen pen_white = new Pen(Color.White, 2);
            Rectangle button = new Rectangle(90, 200, 120, 50);


            string text2 = "  登陆\nDarkcity";
            SizeF sizeF2 = g.MeasureString(text2, new Font("黑体", 14));

            if ((e.Location.X > 90) == (e.Location.X < 210))
            {
                if ((e.Location.Y > 200) == (e.Location.Y < 250))
                {
                    g.DrawRectangle(pen_blue, button);
                    g.FillRectangle(Brushes.Black, new Rectangle(92, 202, 106, 46));
                    g.DrawString(text2, new Font("黑体", 14), Brushes.DarkOrange, new Point(150 - (int)(sizeF2.Width / 2), 208));
                }
                else
                {
                    g.DrawRectangle(pen_white, button);
                    g.FillRectangle(Brushes.Black, new Rectangle(92, 202, 106, 46));

                    g.DrawString(text2, new Font("黑体", 14), Brushes.White, new Point(150 - (int)(sizeF2.Width / 2), 208));

                }

            }
            else
            {
                g.DrawRectangle(pen_white, button);
                g.FillRectangle(Brushes.Black, new Rectangle(92, 202, 106, 46));
                g.DrawString(text2, new Font("黑体", 14), Brushes.White, new Point(150 - (int)(sizeF2.Width / 2), 208));

            }

        }

        private void Form_Mousedown(object sender, MouseEventArgs e)
        {
            //ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }

        

        

        private void Form_Click(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((e.Location.X > 90) == (e.Location.X < 210))
                {
                    if ((e.Location.Y > 200) == (e.Location.Y < 250))
                    {
                        //MessageBox.Show("啊！");
                        bool correctid = true;
                        correctid = CheckID(ID);
                        if (!correctid)
                        {
                            if (!(InputBox("请输入OP密码") == "darkcityadministrator"))
                            {
                                MessageBox.Show("该ID已被封禁！");
                                return;
                            }

                        }
                        
                        string ip = "az250.zicp.net";
                        //MessageBox.Show(ip);
                        var ver = (KMCCC.Launcher.Version)comboBox1.SelectedItem;
                        var result = Program.Core.Launch(new LaunchOptions
                        {
                            Version = ver, //Ver为Versions里你要启动的版本名字
                            MaxMemory = 1024, //最大内存，int类型
                            Authenticator = new OfflineAuthenticator(GetSetting("Darkcity", "Darkcity", "ID")), //离线启动，ZhaiSoul那儿为你要设置的游戏名
                                                                                     //Authenticator = new YggdrasilLogin("邮箱", "密码", true), // 正版启动，最后一个为是否twitch登录
                            Mode = LaunchMode.MCLauncher, //启动模式，这个我会在后面解释有哪几种
                            Server = new ServerInfo { Address = ip,Port=35130 }, //设置启动游戏后，自动加入指定IP的服务器，可以不要
                                                                                    //Size = new WindowSize { Height = 768, Width = 1280 } //设置窗口大小，可以不要
                        });
                        if (!result.Success)
                        {
                            switch (result.ErrorType)
                            {
                                case ErrorType.NoJAVA:
                                    MessageBox.Show("你的Java有异常，请寻求AZ获得帮助。\n详细信息：" + result.ErrorMessage, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    break;
                                case ErrorType.AuthenticationFailed:
                                    MessageBox.Show(this, "正版验证失败！请检查你的账号密码", "账号错误\n详细信息：" + result.ErrorMessage, MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    break;
                                case ErrorType.UncompressingFailed:
                                    MessageBox.Show(this, "可能的多开或文件损坏，请确认文件完整且不要多开\n如果你不是多开游戏的话，请检查libraries文件夹是否完整\n详细信息：" + result.ErrorMessage, "可能的多开或文件损坏", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                    break;
                                default:
                                    MessageBox.Show(this,
                                    result.ErrorMessage + "\n" +
                                    (result.Exception == null ? string.Empty : result.Exception.StackTrace),
                                    "启动错误，请将此窗口截图向开发者寻求帮助");
                                    break;
                            }
                        }
                        else
                        {
                            Environment.Exit(1);
                        }
                    }
                    else
                    {
                         ID  = InputBox("请输入游戏ID", "Darkcity");
                         
                        if (ID !=""){
                            SaveSetting("Darkcity", "Darkcity", "ID", ID);
                            Graphics g;
                            g = this.CreateGraphics();
                            SizeF sizeF = g.MeasureString("欢迎," + ID, new Font("黑体", 14));
                            g.FillRectangle(Brushes.Black, new Rectangle(40, 50, 220, 50));
                            g.DrawString("欢迎," + ID, new Font("黑体", 14), Brushes.White, new Point(150 - (int)sizeF.Width / 2, 50));
                            g.Dispose();
                        }
                    }
                }
                
            }
        }
    }
}
namespace TcpClientExample
{

    public class TcpTimeClient
    {
        public static string DomainIp(string str)
        {
            string _return = "";
            try
            {
                IPHostEntry hostinfo = Dns.GetHostEntry(str);
                IPAddress[] aryIP = hostinfo.AddressList;
                _return = aryIP[0].ToString();

            }
            catch (Exception e)
            {
                _return = e.Message;
            }
            return _return;
        }
        private const int portNum = 13;//服务器端口，可以随意修改 
        private static string hostName = DomainIp("az250.zicp.net");//服务器地址，127.0.0.1指本机 
        [STAThread]
        public static string GetIP()
        {
            try
            {
                TcpClient client = new TcpClient(hostName, portNum);
                NetworkStream ns = client.GetStream();
                byte[] bytes = new byte[1024];
                int bytesRead = ns.Read(bytes, 0, bytes.Length);
                string ip = Encoding.ASCII.GetString(bytes, 0, bytesRead);
                client.Close();
                return ip;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return "";
            }
        }
    }
}